package com.blank.web.utils;

import java.sql.*;

public class DbUtil {
    private static String jdbcName;  //用于描述驱动信息
    private static String dbUrl;  //用于描述用户名信息
    private static String dbUserName;  //用于描述用户名的信息
    private static String dbPassword;  //用于描述密码信息

    static{
        jdbcName = "com.mysql.cj.jdbc.Driver";
        dbUrl = "jdbc:mysql://localhost:3306/db_web?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
        dbUserName = "root";
        dbPassword = "123456";
        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     */

    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        return connection;
    }

    /**
     * 关闭连接
     */
    public static void closeResource(Connection con, PreparedStatement prep) throws SQLException {
        if(null != con){
            con.close();
        }
        if(null != prep){
            prep.close();
        }
    }

}
